Page({
  data: {
    heads: '',
    nickname: '',
    openid: ''
  },

  onLoad: function (options) {
    var that = this;
    wx.getUserProfile({
      desc: '用于展示用户信息',
      success: function (res) {
        var userInfo = res.userInfo;
        var nickName = userInfo.nickName;
        var avatarUrl = userInfo.avatarUrl;
        that.setData({
          heads: avatarUrl,
          nickname: nickName
        });
      }
    });

    wx.login({
      success: function (res) {
        if (res.code) {
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx8602f09b65ad04db&secret=b1d41f683946c86ad52ecd7c25d955c9&js_code=' + res.code + '&grant_type=authorization_code',
            data: {},
            method: 'GET',
            success: function (res) {
              var openid = res.data.openid;
              that.setData({
                openid: openid
              });
            }
          });
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  }
});